<%@page contentType="text/html"%>
<%@page pageEncoding="UTF-8"%>
<%@ page import="java.sql.*, javax.sql.*, java.io.* , javax.naming.*" %>
<html>
<head>
    <title>学生信息</title>
</head>
<body>
<h1>学生信息</h1>
<%
    InitialContext context = new InitialContext();
    DataSource dataSource =
            (DataSource) context.lookup("jdbc/DSTest");
    Connection conn = null;
    Statement stmt = null;
    ResultSet rset = null;
    try {
        conn = dataSource.getConnection();
        stmt = conn.createStatement();
        rset = stmt.executeQuery("select * from student");
        if (rset.next()) {
%>
<table width="100%" border="1" >
    <tr align="left">
        <th>学号</th><th>课程名</th><th>成绩</th>
    </tr>
    <%
        do {
    %>
    <tr>
        <td><%= rset.getString("id") %></td>
        <td><%= rset.getString("course") %></td>
        <td><%= rset.getString("score") %></td>
    </tr>
    <%
        } while (rset.next());
    %>
</table>
<%
} else {
%>
No results from query
<%
    }
} catch (SQLException e) {
%>
<%= e.getMessage() %>
<%
        e.printStackTrace();
    } finally {
        if (rset != null) { rset.close(); }
        if (stmt != null) { stmt.close(); }
        if (conn != null) { conn.close(); }
        if (context != null) { context.close(); }
    }
%>
</body>
</html>

